package org.myspring.test.postprocessor;

import lombok.extern.slf4j.Slf4j;
import org.myspring.BeanPostProcessor;
import org.myspring.annotation.Component;

import java.lang.reflect.Proxy;

@Component
@Slf4j
public class MyBeanPostProcessor implements BeanPostProcessor {

    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        if ("orderService".equals(beanName)) {
            Object o = Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), (proxy, method, args) -> {
                log.info("代理对象创建-beforeMethod-beanName-{},method={}", beanName, method);
                Object invoke = method.invoke(bean, args);
                log.info("代理对象创建-AfterMethod-beanName-{},method={}", beanName, method);
                return invoke;
            });
            return o;
        }
        return bean;
    }

}
